<template>
  <section class="webview fixHeight">
    <el-scrollbar style="height:100%">
      <el-form ref="form" :model="detail" label-width="100px" class="form">
        <el-form-item label="出售标题">
          <el-input v-model="detail.title" />
        </el-form-item>
        <el-form-item label="房屋标签">
          <el-select v-model="detail.tagsType" multiple placeholder="请选择">
            <el-option
              v-for="item in $store.state.user.typeList.filter(item => item.typeSimplify == 'hou_tags_type')"
              :key="item.id"
              :label="item.title"
              :value="item.simplify"
            />
          </el-select>
        </el-form-item>
        <!--        <el-form-item label="看房时间">-->
        <!--          <el-select v-model="detail.tagsType"  placeholder="请选择">-->
        <!--            <el-option-->
        <!--              v-for="item in $store.state.user.typeList.filter(item => item.typeSimplify == 'hou_look_type')"-->
        <!--              :key="item.id"-->
        <!--              :label="item.title"-->
        <!--              :value="item.simplify"-->
        <!--            />-->
        <!--          </el-select>-->
        <!--        </el-form-item>-->
        <el-form-item label="总价">
          <el-input v-model="detail.hopePrice" />
        </el-form-item>
        <el-form-item label="面积">
          <el-input v-model="detail.inArea" />
        </el-form-item>
        <el-form-item label="户型">
          <el-select v-model="detail.apartmentId" placeholder="请选择">
            <el-option
              v-for="item in apartmentIdOptions"
              :key="item.id"
              :label="item.typeName"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <!--        <el-form-item label="户型类型">-->
        <!--          <el-select v-model="detail.apartmentType" placeholder="请选择">-->
        <!--            <el-option-->
        <!--              v-for="item in apartmentTypeOptions"-->
        <!--              :key="item.id"-->
        <!--              :label="item.title"-->
        <!--              :value="item.simplify"-->
        <!--            />-->
        <!--          </el-select>-->
        <!--        </el-form-item>-->
        <el-form-item label="是否新房">
          <el-select v-model="detail.newOrSecond" placeholder="请选择">
            <el-option
              v-for="item in newOrSecondOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="是否有电梯">
          <el-select v-model="detail.isElevator" placeholder="请选择">
            <el-option
              :value="true"
              label="有"
            />
            <el-option
              :value="false"
              label="没有"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="装修类型">
          <el-select v-model="detail.renovationType" placeholder="请选择">
            <el-option
              v-for="item in renovationTypeOptions"
              :key="item.id"
              :label="item.title"
              :value="item.simplify"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="房屋朝向">
			 <el-select v-model="detail.houseOrientation" placeholder="请选择">
			<el-option
			  v-for="item in orientation"
			  :key="item"
			  :label="item"
			  :value="item"
			/>
			</el-select>
        </el-form-item>
        <el-form-item label="房屋类型">
          <el-select v-model="detail.houseType" placeholder="请选择">
            <el-option
              v-for="item in $store.state.user.typeList.filter(item=>item.typeSimplify == 'hou_house_type')"
              :key="item.id"
              :label="item.title"
              :value="item.simplify"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="权属">
          <el-select v-model="detail.ownershipType" placeholder="请选择">
            <el-option
              v-for="item in $store.state.user.typeList.filter(item=>item.typeSimplify == 'hou_ownership_type')"
              :key="item.id"
              :label="item.title"
              :value="item.simplify"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="配套">
          <el-select v-model="detail.houseSupporting" placeholder="请选择" multiple>
            <el-option
              v-for="item in $store.state.user.typeList.filter(item=>item.typeSimplify == 'hou_supporting_type')"
              :key="item.id"
              :label="item.title"
              :value="item.simplify"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="单价">
          <el-input v-model="detail.unitPrice" />
        </el-form-item>
        <el-form-item label="年代">
          <el-input v-model="detail.buildingYear" />
        </el-form-item>
        <el-form-item label="核心卖点">
          <el-input v-model="detail.corePoint" />
        </el-form-item>
        <el-form-item label="业主心态">
          <el-input v-model="detail.ownerMentality" />
        </el-form-item>
        <el-form-item label="所在小区">
          <el-select v-model="detail.residentialId" placeholder="请选择">
            <el-option
              v-for="item in residentialIdOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
		<el-form-item label="楼层">
		  <el-input v-model="detail.floor" />
		</el-form-item>
		<el-form-item label="户室号">
		  <el-input v-model="detail.buildingNum" />
		</el-form-item>
		<el-form-item label="权重">
		  <el-input v-model="detail.sort" />
		</el-form-item>
		<el-form-item label="房源编号">
		  <el-input v-model="detail.ownerMentality" />
		</el-form-item>
        <el-form-item v-if="$route.query.id" label="户型图片">
          <div class="picList">
            <div v-for="(item,index) in resourcesList.filter(item=>item.type =='hou_house_resources_apartment_pictures')" class="item">
              <img class="pic" :src="item.pictureUrl" alt>
              <i class="el-icon-delete delIcon hand" @click="handleRemove(item,index)" />
            </div>
          </div>
          <div class="item itemNo">
            <el-upload
              :action="Is.sevenDomain"
              :multiple="false"
              name="file"
              :limit="1"
              :show-file-list="false"
              :on-success="(res)=>handleSuccess(res,'hou_house_resources_apartment_pictures')"
            >
              <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，只能上传1张，且不超过10M</div>
              <div class="el-upload el-upload--picture-card">
                <i slot="default" class="el-icon-plus" />
              </div>
            </el-upload>
          </div>
        </el-form-item>
        <el-form-item v-if="$route.query.id" label="展示图片">
          <div class="picList">
            <div v-for="(item,index) in resourcesList.filter(item=>item.type == 'hou_house_resources_show_pictures')" class="item">
              <img class="pic" :src="item.pictureUrl" alt>
              <i class="el-icon-delete delIcon hand" @click="handleRemove(item,index)" />
            </div>
          </div>
          <div class="item itemNo">
            <el-upload
              :action="Is.sevenDomain"
              :multiple="false"
              name="file"
              :limit="1"
              :show-file-list="false"
              :on-success="(res)=>handleSuccess(res,'hou_house_resources_show_pictures')"
            >
              <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，只能上传1张，且不超过10M</div>
              <div class="el-upload el-upload--picture-card">
                <i slot="default" class="el-icon-plus" />
              </div>
            </el-upload>
          </div>
        </el-form-item>
        <el-form-item v-if="$route.query.id" label="展示视频">
          <div class="picList">
            <div v-for="(item,index) in resourcesList.filter(item=>item.type == 'hou_house_resources_display_video')" class="item">
              <img class="pic" :src="item.pictureUrl" alt>
              <i class="el-icon-delete delIcon hand" @click="handleRemove(item,index)" />
            </div>
          </div>
          <div class="item itemNo">
            <el-upload
              :action="Is.sevenDomain"
              :multiple="false"
              name="file"
              :limit="1"
              :show-file-list="false"
              :on-success="(res)=>handleSuccess(res,'hou_house_resources_display_video')"
            >
              <div class="el-upload el-upload--picture-card">
                <i slot="default" class="el-icon-plus" />
              </div>
            </el-upload>
          </div>
        </el-form-item>
        <el-form-item label="全景链接">
          <el-input v-model="detail.vrPath" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submit">提交</el-button>
          <el-button @click="cancel">返回</el-button>
          <el-button v-if="$route.query.id"  @click="ok($route.query.id)">通过</el-button>
          <el-button v-if="$route.query.id" @click="back($route.query.id)">打回</el-button>
        </el-form-item>
      </el-form>
    </el-scrollbar>
  </section>
</template>
<script>
import { resourcesAdd, resourcesList, resourcesDelete, apartmentList, residentialList, typeListSon, sellAdd, sellUpdate, sellDetail,updateStatus } from '@/api/api'
export default {
  data() {
    return {
		orientation:['正东','正南','正西','正北','东南','西南','东北','西北'],
      resourcesList: [],
      tagsTypeOptions: [],
      apartmentTypeOptions: [],
      ownershipTypeOptions: [],
      houseTypeOptions: [],
      residentialIdOptions: [],
      apartmentIdOptions: [],
      newOrSecondOptions: [
        {
          value: 1,
          label: '新房'
        },
        {
          value: 2,
          label: '二手房'
        }
      ],
      renovationTypeOptions: [],
      postIdOptions: [
        {
          value: 1,
          label: 'APP端'
        },
        {
          value: 2,
          label: 'PC端'
        }],
      detail: {
		  'sort':0,
		  'floor':0,
		  'buildingNum':0,
		 'orientation':'',
        'agentId': '',
        'apartmentId': '',
        'apartmentType': '',
        'area': 0,
        'buildingNum': '',
        'buildingType': '',
        'buildingYear': '',
        'commissionRate': 0,
        'corePoint': '',
        'developersId': '',
        'floor': 0,
        'hopePrice': 0,
        'houseOrientation': '',
        'houseSupporting': '',
        'houseType': '',
        'housingAgeType': '',
        'housingDescription': '',
        'housingDetails': '',
        'inArea': 0,
        'isElevator': true,
        'isVerify': true,
        'minPrice': 0,
        'newOrSecond': '',
        'onlyHouse': true,
        'openDate': '2020-02-12T01:47:09.516Z',
        'ownerMentality': '',
        'ownershipType': '',
        'propertyType': '',
        'propertyYears': 0,
        'regionId': '',
        'renovationType': '',
        'residentialId': '',
        'resourcesList': [
          {}
        ],
        'serviceIntroduction': '',
        'serviceType': '',
        'subsidiaryInformation': '',
        'tagsType': '',
        'title': '',
        'totalFoor': 0,
        'unitPrice': 0,
        'videoPath': '',
        'vrPath': ''
      }
    }
  },
  created() {
    if (this.$route.query.id) {
      sellDetail(this.$route.query.id).then(({ data }) => {
        this.detail = data
        if (this.detail.tagsType) {
          this.detail.tagsType = this.detail.tagsType.split(',')
        }
        if (this.detail.houseSupporting) {
          this.detail.houseSupporting = this.detail.houseSupporting.split(',')
        }
      })
      resourcesList(this.$route.query.id).then(({ data }) => {
        this.resourcesList = data
      })
    }
    this.getMb()
  },
  methods: {
	  ok(id){
	  		  updateStatus({
	  		    id: id,
	  		    status: 2
	  		  }).then(data => {
	  		    Is.tip('设置成功')
	  		  })
	  },
	  back(id){
	  		  updateStatus({
	  		    id: id,
	  		    status:4
	  		  }).then(data => {
	  		    Is.tip('设置成功')
	  		  })
	  },
    handleRemove(item, index) {
      resourcesDelete([item.id]).then(data => {
        Is.tip('删除成功')
        resourcesList(this.detail.id).then(({ data }) => {
          this.resourcesList = data
        })
      })
    },
    // 上传成功之后的回调
    handleSuccess(res, type) {
      if (res.code === 200) {
        resourcesAdd({
          dataId: this.$route.query.id,
          resourcesList: [{ pictureUrl: res.data, type: type }]
        }).then(data => {
          Is.tip('上传成功')
          resourcesList(this.$route.query.id).then(({ data }) => {
            this.resourcesList = data
          })
        })
      }
    },
    getMb() {
      // 户型列表
      apartmentList({
        curPage: 1,
        pageSize: 999,
        name: ''
      }).then(data => {
        this.apartmentIdOptions = data.data.records
      })
      // 户型类型
      typeListSon('5268de1ed6df5ea239b7e0be7e15bacd').then(({ data }) => {
        this.apartmentTypeOptions = data
      })
      // 装修类型
      typeListSon('d284274dcf28591f7e2eb0d4c96b0ba2').then(({ data }) => {
        this.renovationTypeOptions = data
      })
      // 标签
      typeListSon('ca12c7c1bdc9c80f5dab1c48b9f21b84').then(({ data }) => {
        this.tagsTypeOptions = data
      })
      // // 权属
      // typeListSon('b6699e320a174f88071fe5f81f93b886').then(({ data }) => {
      //   this.ownershipTypeOptions = data
      // })
      // // 房屋类型
      // typeListSon('912692f9210fc09727992bc5fea18521').then(({ data }) => {
      //   this.houseTypeOptions = data
      // })
      // 小区列表
      residentialList({
        curPage: 1,
        pageSize: 999,
        name: ''
      }).then(data => {
        this.residentialIdOptions = data.data.records
      })
    },

    // 添加/编辑
    submit() {
      // 验证不通过则不提交
      if (!this.vertify()) {
        return false
      }
      const param = JSON.parse(JSON.stringify(this.detail))
	  console.log(param)
      // param.tagsType = param.tagsType.join(',')
      if (param.houseSupporting) {
        param.houseSupporting = param.houseSupporting.join(',')
      }
      if (this.$route.query.id) {
        // 编辑
        sellUpdate(param).then(data => {
          Is.tip('编辑成功', 2000, 'success')
          this.$router.back()
        })
      } else {
        // 新增
        sellAdd(param).then(data => {
          Is.tip('添加成功', 2000, 'success')
          this.$router.back()
        })
      }
    },
    // 返回
    cancel() {
      this.$router.back()
    },
    vertify() {
      // if (Is.empty(this.detail.title)) {
      //   Is.tip('请输入标题')
      //   return false
      // }
      return true
    }
  }}
</script>
